//
//  RepairViewController.m
//  Project6
//
//  Created by ToanDK on 11/13/12.
//  Copyright (c) 2012 DungNP. All rights reserved.
//

#import "RepairViewController.h"
#import "RepairScheduledViewController.h"
#import "Repair3ViewController.h"

@interface RepairViewController ()

@end

@implementation RepairViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated
{
    contentScrollView.contentSize = CGSizeMake(320, 500);

    [tabBar setSelectedItem:unscheduledItem];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title =@"Repair";
    // Do any additional setup after loading the view from its nib.
    txtDaveID.placeholder = @"WTG DAVE ID";

    UIImage* buttonImage =[UIImage imageNamed:@"GreenSquareButton"];
    UIImage* buttonImageON =[UIImage imageNamed:@"GreenSquareButtonON"];
    
    [goRepairBtn1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [goRepairBtn1 setBackgroundImage:buttonImageON forState:UIControlStateHighlighted];
    [goRepairBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [goRepairBtn1 setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    [goRepairBtn2 setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [goRepairBtn2 setBackgroundImage:buttonImageON forState:UIControlStateHighlighted];
    [goRepairBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [goRepairBtn2 setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    [goRepairBtn3 setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [goRepairBtn3 setBackgroundImage:buttonImageON forState:UIControlStateHighlighted];
    [goRepairBtn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [goRepairBtn3 setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backgroundTapped)];
    [self.view addGestureRecognizer:tapGesture];
    tapGesture.cancelsTouchesInView = NO;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)backgroundTapped
{
    [self.view endEditing:YES];
}

-(IBAction)goRepair1_tapped:(id)sender
{
    NSLog(@"btnStartRepair_Click");
    NSString *wfID = txtWindFarmTurbineID1.text;
    NSString *wtdID = txtDaveID.text;
    
    //send request to server to get damage report with wfID and wtdID
    
    //  goto repair screen 3
    NSString* damageID;
    [self startRepairForDamageReportWithID:damageID];
}
-(IBAction)goRepair2_tapped:(id)sender
{
    NSLog(@"btnStartRepair_Click");
    //  goto repaire screen 3
    NSString* damageID;
    [self startRepairForDamageReportWithID:damageID];
}
-(IBAction)goRepair3_tapped:(id)sender

{
    NSLog(@"btnStartRepair_Click");
    //  goto repaire screen 3
    NSString* damageID;
    //get damage id from textfield
    damageID = txtDemageReportID.text;
    if ([damageID isEqualToString:@""])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Input damage report ID first" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return;
    }
    [self startRepairForDamageReportWithID:damageID];
}

-(IBAction)btnScanQR_Click:(id)sender
{
    NSLog(@"btnScanQR_Click");
    //  goto repaire screen 3
    NSString* damageID;
    [self startRepairForDamageReportWithID:damageID];
}
-(void)startRepairForDamageReportWithID:(NSString*)damageID
{
    Repair3ViewController * repair3 = [[Repair3ViewController alloc] initWithNibName:@"Repair3ViewController" bundle:nil];
    repair3.damageId = damageID;
    [self.navigationController pushViewController:repair3 animated:YES];
}
// implement textfiled delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - tabbar delegate
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag == 1)
    {
        RepairScheduledViewController * scheduledRepair = [[RepairScheduledViewController alloc] initWithNibName:@"RepairScheduledViewController" bundle:nil];
        [self.navigationController pushViewController:scheduledRepair animated:YES];
   
    }
}
@end
